package com.highcom.service.impl;

import com.highcom.mapper.PermissionDao;
import com.highcom.mapper.RoleDao;
import com.highcom.mapper.UserDao;
import com.highcom.model.Permission;
import com.highcom.model.Role;
import com.highcom.model.User;
import com.highcom.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Set;


/**
 * 用户服务
 */

@Service
public class UserServiceImpl  implements UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao;

    // 根据用户名查询数据库 获取用户信息和相关角色信息,同时需要查询角色相关联的权限信息
    @Override
    public User findByUserName(String username) {
        User user = userDao.findByUserName(username);
        if(user == null){
            return null;
        }

        Integer userId = user.getId();
        // 根据用户ID查询对应角色
        Set<Role> roles = roleDao.findByRoleId(userId);
        for(Role role : roles){
            Integer roleId = role.getId();
            Set<Permission> permissions = permissionDao.findByRoleId(roleId);
            // 让角色关联权限
            role.setPermissions(permissions);
        }
        user.setRoles(roles);
        return user;
    }
}













